package com.cskaoyan.controller.wx;


import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.VoCCY.*;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.service.CartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("wx/cart")
public class WxCartController {
    //private static final Integer userId = 1;

    @Autowired
    CartService cartService;

    @RequestMapping("goodscount")
    public BaseRespVo goodsCount() {
        /*Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer id = user.getId();*/
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();

        int goodscount = cartService.selectCartById(userId);

        return BaseRespVo.ok(goodscount);
    }

    @RequestMapping("add")
    public BaseRespVo add(@RequestBody WxCartAddBo cartAddBo) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();

        return cartService.insertCart(cartAddBo,userId);
    }

    @RequestMapping("index")
    public BaseRespVo index() {
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
        WxCartIndexVo cartIndexVo = cartService.indexCart(userId);

        return BaseRespVo.ok(cartIndexVo);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody WxCartAddBo cartAddBo) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();

        return cartService.updateCart(cartAddBo,userId);
    }

    @RequestMapping("checked")
    public BaseRespVo checked(@RequestBody WxCartCheckedBo cartCheckedBo) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
        WxCartIndexVo cartIndexVo = cartService.updateChecked(cartCheckedBo,userId);

        return BaseRespVo.ok(cartIndexVo);
    }

    @RequestMapping("checkout")
    public BaseRespVo checkout(Integer cartId, Integer addressId, Integer couponId,
                               Integer userCouponId, Integer grouponRulesId) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();

        BaseRespVo checkoutVo = cartService.selectCart(cartId,addressId,couponId,userCouponId,userId,grouponRulesId);

        return checkoutVo;
    }

    @RequestMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody WxCartAddBo cartAddBo) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
        //cartService.insertCart2(cartAddBo,userId);

        return cartService.insertCart2(cartAddBo,userId);
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody WxCartDeleteBo deleteBo) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();
        WxCartIndexVo cartIndexVo = cartService.deleteCart(deleteBo,userId);

        return BaseRespVo.ok(cartIndexVo);
    }

/*    @RequestMapping("checkout1")
    public BaseRespVo checkout1(Integer cartId, Integer addressId, Integer couponId,
                               Integer userCouponId, Integer grouponRulesId) {          //xd购物车

        BaseRespVo checkoutVo = cartService.selectCart2(cartId,addressId,couponId,userCouponId,userId,grouponRulesId);

        return checkoutVo;
    }*/
}
